//
//  MapViewController.m
//  Kfangchan
//
//  Created by mosn on 11/10/14.
//  Copyright (c) 2014 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "MapViewController.h"
#import "BMapKit.h"

@interface MapViewController ()<BMKMapViewDelegate> {
    BMKMapView  *mapCompany;
}

@property (nonatomic) CLLocationCoordinate2D coor;

@end

@implementation MyAnnotation

@synthesize coordinate,subtitle,title;

- (id) initWithCoords:(CLLocationCoordinate2D) coords
{
    self = [super init];
    if (self != nil) {
        self.coordinate = coords;
    }
    return self;
    
}


@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithCOOR:(CLLocationCoordinate2D)coorloc
{
    self=[super initWithNibName:NSStringFromClass([self class]) bundle:nil];
    
    if (self) {
        self.coor = coorloc;
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"楼盘地址"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    mapCompany = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, ScreenHeight)];
    [self.view addSubview:mapCompany];
    
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    
    annotation.coordinate = self.coor;
    mapCompany.zoomLevel = 16;
    [mapCompany addAnnotation:annotation];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [mapCompany viewWillAppear];
    mapCompany.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    [mapCompany setCenterCoordinate:self.coor animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [mapCompany viewWillDisappear];
    mapCompany.delegate = nil; // 不用时，置nil
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"mapidentifer";
    pinView = (MKPinAnnotationView *)[mapCompany dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    
    return pinView;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    mapCompany.delegate = nil;
    
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
